<?php

namespace app\controller;

use app\service\PostBatch;
use support\Request;
use app\service\Post as PostService;
use think\Exception;

class Post
{

    private $post;
    private $postBath;

    public function __construct(PostService $post, PostBatch $postBath)
    {
        $this->post = $post;
        $this->postBath = $postBath;
    }

    /**
     * 创建文章
     * @param Request $request
     * @return \support\Response
     */
    public function create(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $param['data']['tenant_id'] = $request->getTenantId();
        $res = $this->post->create($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 更新文章
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(Request $request)
    {
        $param = $request->all();
        $param['data']['tenant_id'] = $request->getTenantId();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $res = $this->post->update($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 删除文章
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     * @throws \think\db\exception\DbException
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        $res = $this->post->delete($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function select(Request $request)
    {
        $param = $request->all();
        $tenant_id = $request->getTenantId();
        $param['param']['filter']['tenant_id'] = $tenant_id;
        $res = $this->post->select($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 查询单条
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function find(Request $request)
    {
        $param = $request->all();
        $userId = $request->getCurrentUserId();
        $tenantId = $request->getTenantId();
        $data['user_id'] = $userId;
        $param['param']['filter']['tenant_id'] = $tenantId;
        $res = $this->post->find($data, $param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 创建知识本内容页
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     */
    public function createKnowledgeBookPage(Request $request)
    {
        $param = $request->all();
        $res = $this->post->createKnowledgeBookPage($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 更新知识本内容页
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateKnowledgeBookPage(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        try {
            $res = $this->post->updateKnowledgeBookPage($param['data']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 知识本内容页树
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function knowledgeBookPageTree(Request $request)
    {
        $param = $request->all();
        $res = $this->post->knowledgeBookPageTree($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 删除知识本内容页
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteKnowledgeBookPage(Request $request)
    {
        $param = $request->all();
        try {
            $res = $this->post->deleteKnowledgeBookPage($param['param']['filter']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }

    }

    /**
     * 获取知识库内容树
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getKnowledgeBookTree(Request $request)
    {
        $param = $request->all();
        $param['param']['filter']['user_id'] = $request->getCurrentUserId();
        $param['param']['filter']['tenant_id'] = $request->getTenantId();
        $res = $this->post->getKnowledgeBookTree($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 创建知识库内容树
     * @param Request $request
     * @return \support\Response
     */
    public function createKnowledgeBookTree(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $param['data']['tenant_id'] = $request->getTenantId();
        try {
            $res = $this->post->createKnowledgeBookTree($param['data']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新知识库内容树排序
     * @param Request $request
     * @return \support\Response
     */
    public function updateKnowledgeBookTree(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $param['data']['tenant_id'] = $request->getTenantId();
        try {
            $res = $this->post->updateKnowledgeBookTree($param['data']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**删除知识库内容树
     * @param Request $request
     * @return \support\Response
     */
    public function deleteKnowledgeBookTree(Request $request)
    {
        try {
            $param = $request->all();
            $param['param']['filter']['tenant_id'] = $request->getTenantId();
            $res = $this->post->deleteKnowledgeBookTree($param['param']['filter']);
            return json(success_response('', $res));
        } catch (\Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量创建知识库文档/文件
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function postBathCreate(Request $request)
    {
        $param = $request->all();
        $param['data']['user_id'] = $request->getCurrentUserId();
        $param['data']['tenant_id'] = $request->getTenantId();
        $res = $this->postBath->bathCreatePost($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 从catalog 同步树结构 到post
     * @return \support\Response
     */
    public function postSyncTree()
    {
        $this->postBath->bathSyncParentId();
        return json(success_response('', 0));
    }
}
